﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;

namespace WXYW.BLL
{
    public class TFSQService : RepositoryBase
    {
        public SpecialServiceForm GetSpecialServiceForm(long formID)
        {
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == formID).FirstOrDefault();
            if (!form.ApplicationFormReference.IsLoaded)
            {
                form.ApplicationFormReference.Load();
            }
            if (!form.ApplicationForm.UserReference.IsLoaded)
            {
                form.ApplicationForm.UserReference.Load();
            }
            if (!form.SpecialServiceProduct.IsLoaded)
            {
                form.SpecialServiceProduct.Load();
            }
            if (!form.ApplicationForm.UserReference.IsLoaded)
            {
                form.ApplicationForm.UserReference.Load();
            }            
            if (!form.UserReference.IsLoaded)
            {
                form.UserReference.Load();
            }
            if (!form.User1Reference.IsLoaded)
            {
                form.User1Reference.Load();
            }
            if (!form.User2Reference.IsLoaded)
            {
                form.User2Reference.Load();
            }
            if (!form.User3Reference.IsLoaded)
            {
                form.User3Reference.Load();
            }
            if (!form.TFSQContent.IsLoaded)
            {
                form.TFSQContent.Load();
            }
            switch (form.Status)
            {
                case "-1":
                    form.Status = "特服申请-被驳回";
                    break;
                case "1":
                    form.Status = "特服申请-待定义";
                    break;
                case "2":
                    form.Status = "特服申请-待填";
                    break;
                case "3":
                    form.Status = "等待初审人签字";
                    break;
                case "4":
                    form.Status = "等待终审人签字";
                    break;
                case "5":
                    form.Status = "等待加签人签字";
                    break;
                case "6":
                    form.Status = "审批完成";
                    break;
            }
            return form;
        }

        public List<ConfirmQuatationProductModel> GetTFSQProductList(long applicationID)
        {
            List<ConfirmQuatationProductModel> productModels = new List<ConfirmQuatationProductModel>();
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == applicationID).FirstOrDefault();
            if (!form.SpecialServiceProduct.IsLoaded)
            {
                form.SpecialServiceProduct.Load();
            }
            
            foreach (SpecialServiceProduct specialServiceProduct in form.SpecialServiceProduct)
            {
                if (!specialServiceProduct.ApplicationProductReference.IsLoaded)
                {
                    specialServiceProduct.ApplicationProductReference.Load();
                }
                ApplicationProduct product = specialServiceProduct.ApplicationProduct;
                if (!product.ProductTypeReference.IsLoaded)
                {
                    product.ProductTypeReference.Load();
                }
                if(!product.UserReference.IsLoaded)
                {
                    product.UserReference.Load();
                }
                ConfirmQuatationProductModel model = new ConfirmQuatationProductModel();
                model.ApplicationProductID = product.ApplicationProductID;
                model.ProductType = product.ProductType.ProductTypeName;
                model.SerialNumber = product.ProductSerialNumber;
                model.ReapairAccessory = string.Empty;
                model.Cost = 0;
                model.isConfirmRepair = product.IsDecideToRepair;
                model.Engineer = product.User.ContactPerson;
                if (product.ApplySpecialService.HasValue && product.ApplySpecialService.Value)
                {
                    model.ApplySpecialService = true;
                }
                else
                {
                    model.ApplySpecialService = false;
                }

                if (!product.QuatationForm.IsLoaded)
                {
                    product.QuatationForm.Load();
                }
                foreach (QuatationForm quaForm in product.QuatationForm)
                {
                    model.ReapairAccessory += "," + quaForm.AccessoryName;
                    model.Cost += quaForm.Cost;
                    if (quaForm.IsBWBA.HasValue && quaForm.IsBWBA == true)
                    {
                        model.IsBWBA = "保外备案";
                    }
                }
                if (!string.IsNullOrEmpty(model.ReapairAccessory))
                {
                    model.ReapairAccessory = model.ReapairAccessory.Substring(2);
                }

                if (!product.ReturnToFactoryForm.IsLoaded)
                {
                    product.ReturnToFactoryForm.Load();
                }
                if (product.ReturnToFactoryForm.Count > 0)
                {
                    model.IsReturnToFactory = true;
                }
                //
                productModels.Add(model);
                //}
            }

            return productModels;
        }

        public void SetUser(User userEntity, long applicationID)
        {
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == applicationID).FirstOrDefault();
            form.User = userEntity;
            if (!form.DefineDate.HasValue)
            {
                form.DefineDate = DateTime.Now;
            }
            UpdateEntity(form);
        }

        public void SetUser1(User userEntity, long applicationID)
        {
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == applicationID).FirstOrDefault();
            form.User1 = userEntity;
            UpdateEntity(form);
        }

        public void SetUser2(User userEntity, long applicationID)
        {
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == applicationID).FirstOrDefault();
            form.User2 = userEntity;
            UpdateEntity(form);
        }

        public void SetUser3(User userEntity, long applicationID)
        {
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == applicationID).FirstOrDefault();
            form.User3 = userEntity;
            UpdateEntity(form);
        }

        public void UpdateFormStatus(string statusCode, long applicationID)
        {
            SpecialServiceForm form = GetEntities<SpecialServiceForm>().Where(s => s.ID == applicationID).FirstOrDefault();
            form.Status = statusCode;
            UpdateEntity(form);
        }

        public void UpdateForm(SpecialServiceForm form)
        {
            UpdateEntity(form);
        }
    }
}
